﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace SmartTestManager_1._0
{
    public partial class Login : Form
    {
        String ConnectionStr = @"data source=.;initial catalog =TM;Integrated security=true;";
        SqlConnection Connection;
        SqlCommand sqlCmd;
        SqlDataReader DataReader;
        SqlDataAdapter DataAdapter;
        DataSet dataset;
        public Login()
        {
            InitializeComponent();
        }

        private void btnLogin_Click(object sender, EventArgs e)
        {
            Connection = new SqlConnection();
            Connection.ConnectionString = ConnectionStr;

            sqlCmd = new SqlCommand();

            if (rbtnAdmin.Checked)
            {
                try
                {
                    Connection.Open();

                    sqlCmd.Connection = Connection;
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.CommandText = "SELECT  * from Testers where  Name='" + tbxUserName.Text + "' and Passwd= '" + tbxPassword.Text + "'and Flag= 'admin'";

                    DataReader = sqlCmd.ExecuteReader();
                    bool reader_state = DataReader.Read();
                    if (reader_state == false)
                    {
                        MessageBox.Show("Wrong Entry");
                    }
                    else 
                    {
                        adminMainForm aMF = new adminMainForm();
                        this.Hide();
                        aMF.Show();

                    }

                    DataReader.Close();


                }

                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                finally
                {
                    Connection.Close();
                }
            }

            if (rbtnTester.Checked)
            {
                try
                {
                    Connection.Open();

                    sqlCmd.Connection = Connection;
                    sqlCmd.CommandType = CommandType.Text;
                    sqlCmd.CommandText = "SELECT  * from Testers where  Name='" + tbxUserName.Text + "' and Passwd= '" + tbxPassword.Text + "' ";

                    DataReader = sqlCmd.ExecuteReader();
                    bool reader_state = DataReader.Read();
                    if (reader_state == false)
                    {
                        MessageBox.Show("Wrong Entry");
                    }
                    else MessageBox.Show("welcome");

                    DataReader.Close();


                }

                catch (SqlException ex)
                {
                    MessageBox.Show(ex.Message);
                }

                finally
                {
                    Connection.Close();
                }
            }
        }
    }
}
